


//////////////////

//models/Weapons/W_missile_closed.mdl
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

ENT.Dropped = false;
ENT.typ = "weapon"
//models/Items/grenadeAmmo.mdl
function ENT:Initialize()


    self.Entity:SetModel("models/Items/grenadeAmmo.mdl")
	self.Entity:SetAngles(Angle(0,0,0))
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS  )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phy = self.Entity:GetPhysicsObject()
	if phy:IsValid() then phy:Wake() end


	
end

function ENT:PhysicsCollide(data, physobj)

if data.Speed > 60 and data.DeltaTime > 0.3 then
self.Entity:EmitSound("weapons/grenade/hgrenb1a.wav")
else
self.Entity:EmitSound("weapons/grenade/hgrenb2a.wav")
end
	if data.Speed > 75 and data.DeltaTime > 0.3 then
	
			local LastSpeed = data.OurOldVelocity:Length()
			local NewVelocity = physobj:GetVelocity()
			
			local TargetVelocity = NewVelocity + (LastSpeed*0.2)*NewVelocity:GetNormalized()
			physobj:SetVelocity(TargetVelocity)

			//self:Explosion()
			//self.Entity:Remove()
	elseif data.Speed < 8 then
		self:Explosion()
			self.Entity:Remove()
	end
end

function ENT:OnTakeDamage( dmginfo )
end

function ENT:Explosion()
	
	local explo = ents.Create( "env_explosion" )
		explo:SetOwner( self.GrenadeOwner )
		explo:SetPos( self.Entity:GetPos() )
		explo:SetKeyValue( "iMagnitude", "100" )
		explo:Spawn()
		explo:Activate()
		explo:Fire( "Explode", "", 0 )
	
	local shake = ents.Create( "env_shake" )
		shake:SetOwner( self.GrenadeOwner )
		shake:SetPos( self.Entity:GetPos() )
		shake:SetKeyValue( "amplitude", "2000" )
		shake:SetKeyValue( "radius", "400" )
		shake:SetKeyValue( "duration", "2.5" )
		shake:SetKeyValue( "frequency", "255" )
		shake:SetKeyValue( "spawnflags", "4" )
		shake:Spawn()
		shake:Activate()
		shake:Fire( "StartShake", "", 0 )
	
	for k, v in pairs ( ents.FindInSphere( self.Entity:GetPos(), 250 ) ) do
		v:Fire( "EnableMotion", "", math.random( 0, 0.5 ) )
	end
	
end



function ENT:Touch( entity )
	if (entity:IsPlayer()) then 
	self:Explosion()
	self.Entity:Remove()
	end
	

end

